// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import user from './module/user'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
  state: {//存放状态
    cartList: uni.getStorageSync('cartList') || []
  },
  mutations: {
    setCartList(state, cartList) {
      state.cartList = cartList
      uni.setStorageSync("cartList", cartList)
    }
  },
  actions: {},
  getters: {
    allChecked(state) {
      return state.cartList.every(v => v.goods_select)
    },
    selectCartList(state) {
      return state.cartList.filter(item => item.goods_select)
    },
    totalCount(state, getters) {
      // let sum = 0
      // getters.selectCartList.forEach(item => {
      //   sum += item.goods_count
      // })
      // console.log(sum)
      // return sum
      return getters.selectCartList.reduce((sum, item) => sum + item.goods_count, 0)
    },
    totalPrice(state, getters) {
      return getters.selectCartList.reduce((sum, item) => sum + item.goods_count * item.goods_price, 0)
    }
  },
  modules: {
    user
  }
})
export default store